package resources.NTree;

import java.util.Vector;
import resources.NTree.*;

public class NTree {
	Object value;
	Vector des;

	public NTree(Object aValue) {
		value = aValue;
		des = new Vector();
	}

	public NTree() {
		value = "";
		des = new Vector();		
	}
	
	public void setValue(Object anObject)
	{
		this.value=anObject;
	}	
	
	public Object value() {
		return value;
	}
	
	public Vector childs() {
		return des;
	}

	public void addElement(Object aValue) {
		des.addElement(new NTree(aValue));
	}

	public void addNTree(Object aNTree) {
		des.addElement(aNTree);
	}

	
	public void getNodes(SList aList) {
		aList.addElement(value);
		for (int index = 0; index < des.size(); index++)
			((NTree) des.elementAt(index)).getNodes(aList);
	}

	public boolean contains(Object aObject) {
		boolean findet = false;
		int index = 0;

		if (aObject.equals(value))
			return true;
		else {
			while ((!findet) & (index < des.size())) {
				findet = ((NTree) des.elementAt(index)).contains(aObject);
				index++;
			}
			return findet;
		}
	}

	public void getBranchs(SList branchs) {
		Vector aBranch = new Vector();
		this.getBranchs(aBranch, branchs);
	}

	private void getBranchs(Vector aBranch, SList branchs) {
		Vector copyBranch = (Vector) aBranch.clone();
		copyBranch.addElement(value);

		if (des.size() == 0)
			branchs.addElement(copyBranch);
		else
			for (int index = 0; index < des.size(); index++)
				((NTree) des.elementAt(index)).getBranchs(copyBranch, branchs);
	}

	public void addTo(Object aObject, Object aValue) {
		if (value.equals(aObject))
			this.addElement(aValue);
		else
			for (int index = 0; index < des.size(); index++)
				((NTree) des.elementAt(index)).addTo(aObject, aValue);
	}
	
	public void getChildsOf(Object aObject, Vector childs)
	{
		if (value.equals(aObject))
			childs=this.des;
		else
			for (int index = 0; index < des.size(); index++)
				((NTree) des.elementAt(index)).getChildsOf(aObject, childs);
	}
}